local assets =
{
    Asset("ANIM", "anim/sw_piggy_bomb.zip"),
}

-- 一个合并函数
local function CombineTags(tags1, tags2)
    if tags2 ~= nil then
        for _, v in pairs(tags2) do
            table.insert(tags1, v)
        end
    end
    return tags1
end
-- 建筑与伙伴标签,代码作者:梧桐
local function TagsFriendly(othertags)
    return CombineTags({
        "INLIMBO", "NOCLICK", "notarget", "noattack", "playerghost",
        "wall", "structure", "balloon",
        "companion", "glommer", "friendlyfruitfly", "abigail", "shadowminion"
    }, othertags)
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("sw_piggy_bomb")
    inst.AnimState:SetBuild("sw_piggy_bomb")
    inst.AnimState:PlayAnimation("idle_piggy_bomb")

    inst:AddTag("sw_piggy_bomb")   -- 可以装填进猪猪炮中
    inst:AddTag("reloaditem_ammo") -- 动作字符串,直接翻译:重新加载物品弹药

    inst.pickupsound = "rock"

    MakeInventoryFloatable(inst)

    -- 堆叠相关的,也不知道具体什么作用,就留着吧
    inst:AddTag("selfstacker")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    -- 不知道是什么,留着!
    inst:AddComponent("reloaditem")

    inst:AddComponent("stackable")
    inst.components.stackable.maxsize = TUNING.STACK_SIZE_SMALLITEM

    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")

    inst:AddComponent("selfstacker")

    MakeHauntableLaunchAndSmash(inst)

    return inst
end

local function OnHit(inst, attacker, target)
    local fx = SpawnPrefab("sw_buff_piggycorolla_fx")
    if fx ~= nil then
        fx.Transform:SetPosition(target.Transform:GetWorldPosition())
    end
    inst:Remove()
end

local function onthrown(inst, data)
    inst.AnimState:SetOrientation(ANIM_ORIENTATION.OnGround)
end

local function ProjectileFn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeProjectilePhysics(inst)

    inst.AnimState:SetBank("sw_piggy_bomb")
    inst.AnimState:SetBuild("sw_piggy_bomb")
    inst.AnimState:PlayAnimation("dart_piggy_bomb", true)

    inst:AddTag("weapon")

    -- 射弹（来自射弹组件）添加到原始状态以进行优化。
    inst:AddTag("projectile")

    inst:AddTag("NOCLICK")
    inst:AddTag("NOBLOCK")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst.persists = false

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(80)

    inst:AddComponent("projectile")
    inst.components.projectile:SetSpeed(25)
    inst.components.projectile:SetHoming(false)
    inst.components.projectile:SetOnHitFn(OnHit)
    inst.components.projectile:SetHitDist(1.5)
    inst.components.projectile:SetLaunchOffset(Vector3(2.5, 0.75, 2.5))
    inst.components.projectile:SetOnMissFn(inst.Remove)
    inst.components.projectile.range = 30
    inst.components.projectile.has_damage_set = true
    inst:ListenForEvent("onthrown", onthrown) -- 监听事件:动画射出的方向

    return inst
end

local function OnHit1(inst, attacker, target)
    local x, y, z = target.Transform:GetWorldPosition()
    local fx = SpawnPrefab("sw_buff_piggycorolla_fx")
    if fx ~= nil then
        fx.Transform:SetPosition(x, y, z)
    end

    local friendlyTags = TagsFriendly(TheNet:GetPVPEnabled() and {} or { "player" })
    -- 查找范围4加上物理半径内的所有实体，排除友好单位
    local entities = TheSim:FindEntities(x, y, z, 4, nil, friendlyTags)
    for _, entity in ipairs(entities) do
        if entity ~= attacker and entity ~= target and entity.components.combat then
            entity.components.combat:GetAttacked(inst or attacker, 40, nil, nil)
            if entity.components.health and entity.components.health:IsDead() then
                attacker:PushEvent("killed", { victim = entity, attacker = attacker })
                if entity.components.combat.onkilledbyother ~= nil then
                    entity.components.combat.onkilledbyother(entity, attacker)
                end
            end
        end
    end

    inst:Remove()
end

local function ProjectileFn1()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeProjectilePhysics(inst)

    inst.AnimState:SetBank("sw_piggy_bomb")
    inst.AnimState:SetBuild("sw_piggy_bomb")
    inst.AnimState:PlayAnimation("dart_piggy_bomb", true)

    inst:AddTag("weapon")

    -- 射弹（来自射弹组件）添加到原始状态以进行优化。
    inst:AddTag("projectile")

    inst:AddTag("NOCLICK")
    inst:AddTag("NOBLOCK")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst.persists = false

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(40)

    inst:AddComponent("projectile")
    inst.components.projectile:SetSpeed(25)
    inst.components.projectile:SetHoming(false)
    inst.components.projectile:SetOnHitFn(OnHit1)
    inst.components.projectile:SetHitDist(1.5)
    inst.components.projectile:SetLaunchOffset(Vector3(2.5, 0.75, 2.5))
    inst.components.projectile:SetOnMissFn(inst.Remove)
    inst.components.projectile.range = 30
    inst.components.projectile.has_damage_set = true
    inst:ListenForEvent("onthrown", onthrown) -- 监听事件:动画射出的方向

    return inst
end

local function fn_enlight()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("sw_piggy_bomb")
    inst.AnimState:SetBuild("sw_piggy_bomb")
    inst.AnimState:PlayAnimation("idle_enlight_piggy_bomb")

    inst:AddTag("sw_piggy_bomb")   -- 可以装填进猪猪炮中
    inst:AddTag("reloaditem_ammo") -- 动作字符串,直接翻译:重新加载物品弹药

    inst.pickupsound = "rock"

    MakeInventoryFloatable(inst)

    -- 堆叠相关的,也不知道具体什么作用,就留着吧
    inst:AddTag("selfstacker")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    -- 不知道是什么,留着!
    inst:AddComponent("reloaditem")

    inst:AddComponent("stackable")
    inst.components.stackable.maxsize = TUNING.STACK_SIZE_SMALLITEM

    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")

    inst:AddComponent("selfstacker")

    MakeHauntableLaunchAndSmash(inst)

    return inst
end


local function ProjectileFn_enlight()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeProjectilePhysics(inst)

    inst.AnimState:SetBank("sw_piggy_bomb")
    inst.AnimState:SetBuild("sw_piggy_bomb")
    inst.AnimState:PlayAnimation("dart_enlight_piggy_bomb", true)

    inst:AddTag("weapon")

    -- 射弹（来自射弹组件）添加到原始状态以进行优化。
    inst:AddTag("projectile")

    inst:AddTag("NOCLICK")
    inst:AddTag("NOBLOCK")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst.persists = false

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(100)

    inst:AddComponent("planardamage")
    inst.components.planardamage:SetBaseDamage(50)

    inst:AddComponent("damagetypebonus")
    inst.components.damagetypebonus:AddBonus("shadow_aligned", inst, 1.1, "sw_enlight_piggy_bomb")

    inst:AddComponent("projectile")
    inst.components.projectile:SetSpeed(25)
    inst.components.projectile:SetHoming(false)
    inst.components.projectile:SetOnHitFn(OnHit)
    inst.components.projectile:SetHitDist(1.5)
    inst.components.projectile:SetLaunchOffset(Vector3(2.5, 0.75, 2.5))
    inst.components.projectile:SetOnMissFn(inst.Remove)
    inst.components.projectile.range = 30
    inst.components.projectile.has_damage_set = true
    inst:ListenForEvent("onthrown", onthrown) -- 监听事件:动画射出的方向

    return inst
end

local function OnHit2(inst, attacker, target)
    local x, y, z = target.Transform:GetWorldPosition()
    local spdamage = {
        planar = 10,
    }
    local fx = SpawnPrefab("sw_buff_piggycorolla_fx")
    if fx ~= nil then
        fx.Transform:SetPosition(x, y, z)
    end

    local friendlyTags = TagsFriendly(TheNet:GetPVPEnabled() and {} or { "player" })
    -- 查找范围4加上物理半径内的所有实体，排除友好单位
    local entities = TheSim:FindEntities(x, y, z, 4, nil, friendlyTags)
    for _, entity in ipairs(entities) do
        if entity ~= attacker and entity ~= target and entity.components.combat then
            entity.components.combat:GetAttacked(inst or attacker, 50, nil, nil, spdamage)
            if entity.components.health and entity.components.health:IsDead() then
                attacker:PushEvent("killed", { victim = entity, attacker = attacker })
                if entity.components.combat.onkilledbyother ~= nil then
                    entity.components.combat.onkilledbyother(entity, attacker)
                end
            end
        end
    end

    inst:Remove()
end

local function ProjectileFn_enlight1()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeProjectilePhysics(inst)

    inst.AnimState:SetBank("sw_piggy_bomb")
    inst.AnimState:SetBuild("sw_piggy_bomb")
    inst.AnimState:PlayAnimation("dart_enlight_piggy_bomb", true)

    inst:AddTag("weapon")

    -- 射弹（来自射弹组件）添加到原始状态以进行优化。
    inst:AddTag("projectile")

    inst:AddTag("NOCLICK")
    inst:AddTag("NOBLOCK")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst.persists = false

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(50)

    inst:AddComponent("planardamage")
    inst.components.planardamage:SetBaseDamage(10)

    inst:AddComponent("damagetypebonus")
    inst.components.damagetypebonus:AddBonus("shadow_aligned", inst, 1.1, "sw_enlight_piggy_bomb")

    inst:AddComponent("projectile")
    inst.components.projectile:SetSpeed(25)
    inst.components.projectile:SetHoming(false)
    inst.components.projectile:SetOnHitFn(OnHit2)
    inst.components.projectile:SetHitDist(1.5)
    inst.components.projectile:SetLaunchOffset(Vector3(2.5, 0.75, 2.5))
    inst.components.projectile:SetOnMissFn(inst.Remove)
    inst.components.projectile.range = 30
    inst.components.projectile.has_damage_set = true
    inst:ListenForEvent("onthrown", onthrown) -- 监听事件:动画射出的方向

    return inst
end

local function fn_shadow()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("sw_piggy_bomb")
    inst.AnimState:SetBuild("sw_piggy_bomb")
    inst.AnimState:PlayAnimation("idle_shadow_piggy_bomb")

    inst:AddTag("sw_piggy_bomb")   -- 可以装填进猪猪炮中
    inst:AddTag("reloaditem_ammo") -- 动作字符串,直接翻译:重新加载物品弹药

    inst.pickupsound = "rock"

    MakeInventoryFloatable(inst)

    -- 堆叠相关的,也不知道具体什么作用,就留着吧
    inst:AddTag("selfstacker")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    -- 不知道是什么,留着!
    inst:AddComponent("reloaditem")

    inst:AddComponent("stackable")
    inst.components.stackable.maxsize = TUNING.STACK_SIZE_SMALLITEM

    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")

    inst:AddComponent("selfstacker")

    MakeHauntableLaunchAndSmash(inst)

    return inst
end


local function ProjectileFn_shadow()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeProjectilePhysics(inst)

    inst.AnimState:SetBank("sw_piggy_bomb")
    inst.AnimState:SetBuild("sw_piggy_bomb")
    inst.AnimState:PlayAnimation("dart_shadow_piggy_bomb", true)

    inst:AddTag("weapon")

    -- 射弹（来自射弹组件）添加到原始状态以进行优化。
    inst:AddTag("projectile")

    inst:AddTag("NOCLICK")
    inst:AddTag("NOBLOCK")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst.persists = false

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(100)

    inst:AddComponent("planardamage")
    inst.components.planardamage:SetBaseDamage(50)

    inst:AddComponent("damagetypebonus")
    inst.components.damagetypebonus:AddBonus("lunar_aligned", inst, 1.1, "sw_enlight_piggy_bomb")

    inst:AddComponent("projectile")
    inst.components.projectile:SetSpeed(25)
    inst.components.projectile:SetHoming(false)
    inst.components.projectile:SetOnHitFn(OnHit)
    inst.components.projectile:SetHitDist(1.5)
    inst.components.projectile:SetLaunchOffset(Vector3(2.5, 0.75, 2.5))
    inst.components.projectile:SetOnMissFn(inst.Remove)
    inst.components.projectile.range = 30
    inst.components.projectile.has_damage_set = true
    inst:ListenForEvent("onthrown", onthrown) -- 监听事件:动画射出的方向

    return inst
end

local function ProjectileFn_shadow1()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeProjectilePhysics(inst)

    inst.AnimState:SetBank("sw_piggy_bomb")
    inst.AnimState:SetBuild("sw_piggy_bomb")
    inst.AnimState:PlayAnimation("dart_shadow_piggy_bomb", true)

    inst:AddTag("weapon")

    -- 射弹（来自射弹组件）添加到原始状态以进行优化。
    inst:AddTag("projectile")

    inst:AddTag("NOCLICK")
    inst:AddTag("NOBLOCK")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst.persists = false

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(50)

    inst:AddComponent("planardamage")
    inst.components.planardamage:SetBaseDamage(10)

    inst:AddComponent("damagetypebonus")
    inst.components.damagetypebonus:AddBonus("lunar_aligned", inst, 1.1, "sw_enlight_piggy_bomb")

    inst:AddComponent("projectile")
    inst.components.projectile:SetSpeed(25)
    inst.components.projectile:SetHoming(false)
    inst.components.projectile:SetOnHitFn(OnHit2)
    inst.components.projectile:SetHitDist(1.5)
    inst.components.projectile:SetLaunchOffset(Vector3(2.5, 0.75, 2.5))
    inst.components.projectile:SetOnMissFn(inst.Remove)
    inst.components.projectile.range = 30
    inst.components.projectile.has_damage_set = true
    inst:ListenForEvent("onthrown", onthrown) -- 监听事件:动画射出的方向

    return inst
end

return Prefab("sw_piggy_bomb", fn, assets),
    Prefab("sw_piggy_bomb_proj", ProjectileFn, assets),
    Prefab("sw_piggy_bomb_proj1", ProjectileFn1, assets),
    Prefab("sw_enlight_piggy_bomb", fn_enlight, assets),
    Prefab("sw_enlight_piggy_bomb_proj", ProjectileFn_enlight, assets),
    Prefab("sw_enlight_piggy_bomb_proj1", ProjectileFn_enlight1, assets),
    Prefab("sw_shadow_piggy_bomb", fn_shadow, assets),
    Prefab("sw_shadow_piggy_bomb_proj", ProjectileFn_shadow, assets),
    Prefab("sw_shadow_piggy_bomb_proj1", ProjectileFn_shadow1, assets)
